<template>
  <div>
    <div class="navigation-bar-container">
      <!-- 外部插入 -->
      <div class="navigation-bar-out">
        <slot></slot>
      </div>
      <ul class="navigation-bar-list left">
        <!-- 返回 -->
        <li class="navigation-bar-item" @click="backClick" v-if="back">
          <!-- 图片 -->
          <img
            src="@/assets/images/back.png"
            class="navigation-bar-item-image navigation-bar-item-image-back"
            @load="imageOnLoad($event)"
          />
        </li>
        <!-- 左边按钮 -->
        <li
          class="navigation-bar-item"
          v-for="item in (typeof left == 'string' && left != '' ? [left] : left)"
          :key="item"
          @click="navigationBarClick(item)"
        >
          <!-- 文字 -->
          <span v-if="item.indexOf('.png') == -1" class="navigation-bar-item-text">{{ item }}</span>
          <!-- 图片 -->
          <img
            v-else
            :src="item.split(';')[0] | imagePath"
            :style="{width: item.split(';')[1]}"
            class="navigation-bar-item-image"
            @load="imageOnLoad($event)"
          />
        </li>
        <slot name="left"></slot>
      </ul>
      <div class="navigation-bar-center">
        <span class="navigation-bar-title">{{ title }}</span>
        <slot name="center"></slot>
      </div>
      <ul class="navigation-bar-list right">
        <!-- 右边按钮 -->
        <li
          class="navigation-bar-item"
          v-for="item in (typeof right == 'string' && right != '' ? [right] : right)"
          :key="item"
          @click="navigationBarClick(item)"
        >
          <!-- 文字 -->
          <span v-if="item.indexOf('.png') == -1" class="navigation-bar-item-text">{{ item }}</span>
          <!-- 图片 -->
          <img
            v-else
            :src="item.split(';')[0] | imagePath"
            :style="{width: item.split(';')[1]}"
            class="navigation-bar-item-image"
            @load="imageOnLoad($event)"
          />
        </li>
        <slot name="right"></slot>
      </ul>
    </div>
    <p class="navigation-bar-placeholder"></p>
  </div>
</template>
<script>
export default {
  name: "navigation-bar",
  data() {
    return {
      // 屏幕宽度
      screenWidth:
        window.innerWidth ||
        document.documentElement.clientWidth ||
        document.body.clientWidth
    };
  },
  props: {
    title: String, // 标题
    back: Boolean, // 是否有返回按钮
    left: [Array, String], // 左边按钮
    right: [Array, String], // 右边按钮
    callback: Function // 点击按钮的回调函数
  },
  methods: {
    // 图片加载
    imageOnLoad(e) {
      return; //暂时不用以下方式，原因是iOS上不执行
      var element = e.path[0];
      element.style.width = element.naturalWidth / 75 + "rem";
      element.style.height = element.naturalHeight / 75 + "rem";
      element.style.opacity = 1;
    },
    // 返回
    backClick() {
      this.$.router.pop();
    },
    // 导航栏点击事件
    navigationBarClick(value) {
      value = value.split(";")[0];
      if (this.callback) this.callback(value);
    }
  }
};
</script>
<style scoped>
.navigation-bar-placeholder {
  height: 1.17rem;
  width: 100%;
  border-bottom: 1px solid #eee;
}
.navigation-bar-container {
  z-index: 10;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 1.17rem;
  width: 100%;
  background-color: white;
  display: flex;
  align-items: center;
}
.navigation-bar-center {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.navigation-bar-title {
  font-size: 0.42rem;
  color: #333;
  font-weight: bold;
  text-align: center;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.navigation-bar-list {
  position: relative;
  height: 100%;
  display: flex;
  align-items: center;
  flex: 1;
}
.navigation-bar-list.left {
  padding-left: 0.2rem;
  text-align: left;
  justify-content: flex-start;
}
.navigation-bar-list.right {
  padding-right: 0.2rem;
  text-align: right;
  justify-content: flex-end;
}
.navigation-bar-item {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 0 0.2rem;
}
.navigation-bar-item-text {
  font-size: 0.37rem;
}
.navigation-bar-item-image {
  opacity: 1;
}
.navigation-bar-item-image-back {
  width: 0.21rem;
  opacity: 1;
}
.navigation-bar-out {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
</style>